var api = require('../config/api.js');
function sendData(data){
  console.log(wx.getStorageSync('openid'))
  data.OpenId = wx.getStorageSync('openid')
  return new Promise(function (resolve, reject){
    wx.request({
      url: api.SendData,
      data:data,
      method:'post',
      success: function (res) {
        switch (res.statusCode) {
          case 200:
            var resData = res.data.data;//获取返回的数据
            resolve(res.data.data);//返回成功信息
            break;
          default:
            reject(res);
        }
      },
      fail: function (err) {
        reject(err);
      }
    })
  })
}

function wxlogin(){
  return new Promise(function (resolve, reject){
  wx.login({
    success(res){
      if(res.code){
        var code = res.code;
        // 已经授权，可以直接调用 getUserInfo 获取头像昵称
        wx.getUserInfo({
          success: function(secondres) {
            var islogin = wx.getStorageSync('islogin');
            if(!islogin){
              // console.log(secondres);
              wx.setStorageSync('userinfo', JSON.parse(secondres.rawData));
              // wx.setStorageSync('islogin', true);
              secondres["code"] = code;
              wx.request({
                url: api.userLogin,
                data:secondres,
                method:'post',
                success:function(respon){
                  wx.setStorageSync('openid', respon.data.OpenID);
                  wx.setStorageSync('unionid', respon.data.UnionID);
                  console.log("服务端返回的")
                  console.log(wx.getStorageSync('openid'))
                  wx.setStorageSync('islogin', true);
                },
                fail:function(err){
                  console.log(err)
                }
              });
            }
            resolve("success");
          },
          fail:function(err){
            console.log(err)
          }
        })
      }else{
        console.log("没有得到临时code");
        reject("失败")
      }
    },
    fail(err){
      console.log(err)
    }
  });
})}

function mpLogin(){
  return new Promise(function (resolve, reject){
    wx.getSetting({
      success (res){
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
              wx.getUserInfo({
                success: function(res) {
                  console.log(res.userInfo)
                  sendUserInfo(res)
                  resolve(res.userInfo)
                }
              })
        }else{
          reject({})
        }
      }
    });
  })
}
function sendUserInfo(data){
  return new Promise(function (resolve,reject){
      wx.request({
        url: api.SendUserInfo,
        data:data,
        method:'post',
        success:function(res){
          if(res.statusCode == 200){
            resolve("成功")
            console.log(res.data.data)
          }
        },
        fail:function(err){
          reject(err)
        }
      })
    }
  )
}

function findMyHistory(openid){
  return new Promise(function (resolve,reject){
    wx.request({
      url: api.findmyhistory,
      method:'get',
      data:{openId:openid},
      success:function(res){
        console.log("返回的历史数据为");
        console.log(res.data);
        if(res.statusCode == 200){
          resolve(res.data);
        }
      },
      fail:function(err){
        reject(err);
      }
    });
  });
}

function findMyHsitoryDetails(id){
  return new Promise(function(resolve,reject){
    wx.request({
      url: api.findmyhsitorydetails,
      method:'get',
      data:{userQueryId:id},
      success:function(res){
        console.log(res.data);
        resolve(res);
      },
      fail:function(err){
        reject(err);
      }
    })
  });
}
module.exports = {
  sendData,
  mpLogin,
  wxlogin,
  findMyHistory,
  findMyHsitoryDetails
}